<template>
  <div class="app-container">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select filterable placeholder="选择门店" v-model="formInline.store_id">
            <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="width:100px">
          <el-select filterable v-model="store_type_id">
            <el-option
              v-for="item in typeList"
              :key="item.type_id"
              :label="item.type_name"
              :value="item.type_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.gift_id"
            placeholder="请输入礼品编号/礼品名称"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" border size="small">
        <el-table-column align="center" label="商品图片" width="80">
          <template slot-scope="scope">
            <img :src="scope.row.thumb" width="60" height="60" alt />
          </template>
        </el-table-column>
        <el-table-column align="center" label="商品名称" prop="gift_name"></el-table-column>
        <el-table-column align="center" label="新商品编号" prop="gift_id"></el-table-column>
        <el-table-column align="center" label="神采编号" prop="no_obsolete"></el-table-column>
        <el-table-column align="center" label="商品类别" prop="gift_type"></el-table-column>
        <el-table-column align="center" label="商品库存" prop="num"></el-table-column>
        <el-table-column align="center" label="指导价" prop="guidance_money"></el-table-column>
        <el-table-column align="center" label="商品售价" prop="sale_money"></el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.is_online==0"
              size="mini"
              @click="on_off_line(scope.row.gift_id,scope.row.store_id,scope.row.is_online,scope.$index)"
            >已下架</el-button>
            <el-button
              v-else
              size="mini"
              type="success"
              @click="on_off_line(scope.row.gift_id,scope.row.store_id,scope.row.is_online,scope.$index)"
            >已上架</el-button>
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.is_online == 1"
              @click="editClick(scope.row.gift_id,scope.row.store_id,scope.$index,scope.row.sale_money)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <!-- <el-button :disabled="scope.row.is_online == 1" type="danger" icon="el-icon-delete" size="mini" @click="delClick(scope.row.gift_id,scope.row.store_id,scope.$index)"></el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      width="420px"
      title="商品编辑"
      :visible.sync="dialogFormVisible"
      @close="callOf('saveForm')"
    >
      <el-form :rules="rules" :model="saveForm" ref="saveForm" class="markForm">
        <el-form-item label="门店商品实际单个零售价" prop="sale_money">
          <el-input size="small" v-model="saveForm.sale_money" class="w200"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="savemoney('saveForm')">确定</el-button>
      </div>
    </el-dialog>
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
      <!-- <el-button class="pull-right" type="primary" size="small" @click="exportClick()">导出表格</el-button> -->
    </div>
  </div>
</template>

<script>
import {
  getBrandList,
  getStoreList,
  getPage,
  getList,
  getSaleList,
  getSalePage,
  edit_money,
  save,
  del,
  sale_line,
  on_off,
} from "@/api/service/gift_list.js";
import { money } from "@/utils/validate";
const validateVal = (rule, value, callback) => {
  if (!money(value)) {
    callback(new Error("　请输入正确的金额,最多保留两位小数"));
  } else if (value * 1 > 99999) {
    callback(new Error("　请输入正确的金额,最大值为99999.00"));
  } else {
    callback();
  }
};
export default {
  data() {
    return {
      listLoading: false,
      currentPage: 1,
      totalDataNumber: 0,
      editIndex: "",
      store_type_id: "商品销售",
      formInline: {
        store_id: "",
        gift_id: "",
      },
      storeList: [],
      list: null,
      saveForm: {
        gift_id: "",
        sale_money: "",
        store_id: "",
      },
      typeList: [
        { type_id: 0, type_name: "商品销售" },
        { type_id: 1, type_name: "物品销售" },
      ],
      rules: {
        sale_money: [
          { required: true, trigger: "blur", validator: validateVal },
        ],
      },
      dialogFormVisible: false,
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      vm.getStore();
    });
  },
  methods: {
    // getBrand() {
    //     getBrandList({ org: 1, category_id: 241 }).then(response => {
    //         this.brandList = response;
    //         this.formInline.brand = response[0].id
    //         this.getStore()
    //     });
    // },
    getStore() {
      getStoreList({ category_id: 241 }).then((response) => {
        this.storeList = response;
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        }
        if (this.list == null) {
          this.fetchData();
        }
      });
    },
    on_off_line(gift_id, store_id, is_online, index) {
      let text = is_online == 1 ? "确认下架" : "确认上架";
      let line = is_online == 1 ? 0 : 1;
      this.$confirm(text, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.store_type_id == "1") {
          sale_line({
            accessories_id: gift_id,
            store_id,
            is_online: line,
          }).then((response) => {
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
            if (response.code == 1) {
              this.list[index].is_online = is_online == 1 ? 0 : 1;
            }
          });
        } else {
          on_off({
            gift_id: gift_id,
            store_id: store_id,
            is_online: line,
          }).then((response) => {
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
            if (response.code == 1) {
              this.list[index].is_online = is_online == 1 ? 0 : 1;
            }
          });
        }
      });
    },
    savemoney(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          if (this.store_type_id == "1") {
            console.log(this.store_type_id);
            let data = {};
            data.store_id = this.formInline.store_id;
            data.accessories_id = this.saveForm.gift_id;
            data.sale_money = this.saveForm.sale_money;
            edit_money(data).then((response) => {
              this.$message({
                type: response.code == 1 ? "success" : "error",
                message: response.message,
              });
              if (response.code == 1) {
                this.list[this.editIndex].sale_money = data.sale_money;
                this.dialogFormVisible = false;
              }
            });
          } else {
            save(this.saveForm).then((response) => {
              this.$message({
                type: response.code == 1 ? "success" : "error",
                message: response.message,
              });
              if (response.code == 1) {
                this.list[this.editIndex].sale_money = this.saveForm.sale_money;
                this.dialogFormVisible = false;
              }
            });
          }
        }
      });
    },
    searchClick() {
      this.fetchData();
    },
    editClick(id, store_id, index, sale_money) {
      if (this.$refs["saveForm"] !== undefined) {
        this.$refs["saveForm"].resetFields();
      }
      this.editIndex = index;
      this.saveForm.gift_id = id;
      this.saveForm.store_id = store_id;
      this.saveForm.sale_money = sale_money;
      this.dialogFormVisible = true;
    },
    delClick(gift_id, store_id, index) {
      this.$confirm("确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del({ gift_id: gift_id, store_id: store_id }).then((response) => {
          this.$message({
            type: response.code == 1 ? "success" : "error",
            message: response.message,
          });
          if (response.code == 1) {
            this.list.splice(index, 1);
          }
        });
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      if (this.store_type_id == "1") {
        let data = {};
        data.store_id = this.formInline.store_id;
        data.accessories_id = this.formInline.gift_id;
        getSalePage(data)
          .then((response) => {
            this.totalDataNumber = response * 20;
            getSaleList(data).then((response) => {
              // console.log(response)
              response.forEach((val) => {
                if (!val.no_obsolete) val.no_obsolete = "-";
                val.gift_id = val.accessories_id;
                val.gift_type = val.accessories_type;
              });
              this.list = response;
              this.listLoading = false;
            });
          })
          .catch((res) => {
            this.listLoading = false;
          });
      } else {
        getPage(this.formInline)
          .then((response) => {
            this.totalDataNumber = response * 20;
            getList(this.formInline).then((response) => {
              response.forEach((val) => {
                if (!val.no_obsolete) val.no_obsolete = "-";
              });
              this.list = response;
              this.listLoading = false;
            });
          })
          .catch((res) => {
            this.listLoading = false;
          });
      }
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      if (this.store_type_id == "1") {
        let data = {};
        data.store_id = this.formInline.store_id;
        data.accessories_id = this.formInline.gift_id;
        getSaleList(data, val)
          .then((response) => {
            // console.log(response)
            response.forEach((val) => {
              if (!val.no_obsolete) val.no_obsolete = "-";
              val.gift_id = val.accessories_id;
              val.gift_type = val.accessories_type;
            });
            this.list = response;
            this.listLoading = false;
          })
          .catch((res) => {
            this.listLoading = false;
          });
      } else {
        getList(this.formInline, val)
          .then((response) => {
            if (response.length > 0) {
              this.car_gift_num = response[0].car_gift_num;
            }
            response.forEach((val) => {
              if (!val.no_obsolete) val.no_obsolete = "-";
            });
            this.list = response;
            this.listLoading = false;
          })
          .catch((res) => {
            this.listLoading = false;
          });
      }
    },
    callOf() {
      // this.searchClick();
    },
  },
};
</script>